﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CardCharger.lib;

namespace CardCharger.forms
{
    public partial class DeviceSetting : Form
    {

        BC750_CardReader bc750_CardReader;
        CardOS cos;
        UserCardIssue uci;
        IntPtr device = new IntPtr(-1);
        string deviceName = "BC_750";

        public DeviceSetting()
        {
            InitializeComponent();

            /////////////////////////////////////////
            //bc750
            bc750_CardReader = new BC750_CardReader();
            cos = new CardOS(bc750_CardReader as ICardReader);

            this.CB_Type.SelectedIndexChanged += CB_Type_SelectedIndexChanged;

        }

        void CB_Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (device.ToInt32() > 0) { Close();}
            deviceName = CB_Type.Text; 
            button1_Click(this, null);
        }

        IntPtr Open()
        {
            switch (deviceName) { 
            
                case "BC_750":
                    device =  cos.icardReader.CardReaderOpen("usb1");
                    return device;
                case "CRW2810":
                    return new IntPtr(-1);
                default:
                    return new IntPtr(-1);
            }
        
        }

        void Close()
        {
            switch (deviceName)
            {
                case "BC_750":
                    cos.icardReader.CardReaderClose();
                    device = new IntPtr(-1);
                    return;
                case "CRW2810":
                    return ;
                default:
                    return ;
            }
        }




        private void button1_Click(object sender, EventArgs e)
        {
            if (device.ToInt32() < 0)
            {

                device = Open();
                if (device.ToInt32() > 0) { this.button1.Text = "关闭设备"; CB_Type.Enabled = false; }
            }
            else {
                Close();
                this.button1.Text = "打开设备";
                this.CB_Type.Enabled = true;
            }
        }


        public CardOS Cos() { return cos; }
    }
}
